package com.infore.authorityManagement.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.authorityManagement.entity.UserLoginInfo;
import com.infore.authorityManagement.repository.IUserLoginInfoRepository;
import com.infore.authorityManagement.service.IUserLoginInfoService;
import com.infore.authorityManagement.util.Page;
import com.infore.authorityManagement.util.PageUtil;


@Service
public class UserLoginInfoServiceImpl implements IUserLoginInfoService {
    
    @Autowired
    IUserLoginInfoRepository userLoginInfoRepository;
    
    private Specification<UserLoginInfo> createSpecification(UserLoginInfo loginInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = loginInfo.getStartTime(); // 开始时间(作为查询参数)
        String endTime = loginInfo.getEndTime(); // 结束时间(作为查询参数)
        Specification<UserLoginInfo> specification = new Specification<UserLoginInfo>() {
            @Override
            public Predicate toPredicate(Root<UserLoginInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                try {
                    if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime)) {
                        Date startDate = sdf.parse(startTime);
                        Date endDate = sdf.parse(endTime);
                        predicates.add(cb.between(root.get("loginTime"), startDate, endDate));
                    }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!StringUtils.isBlank(loginInfo.getUserName())) {
                    predicates.add(cb.or(cb.like(root.get("userName"), "%" + loginInfo.getUserName() + "%"),
                            cb.like(root.get("userAccount"), "%" + loginInfo.getUserName() + "%")));
                }
                Predicate[] p = new Predicate[predicates.size()];
                query.where(cb.and(predicates.toArray(p)));
                query.orderBy(cb.asc(root.get("loginTime")));
                return query.getRestriction();
            }

        };
        return specification;
    }
    
    @Override
    public List<UserLoginInfo> getLoginInfo(UserLoginInfo loginInfo) {
        Specification<UserLoginInfo> specification = this.createSpecification(loginInfo);
        return userLoginInfoRepository.findAll(specification);
    }

    @Override
    public Page<UserLoginInfo> getLoginInfoForPage(UserLoginInfo loginInfo, Integer pageSize, Integer pageNo) {
        Specification<UserLoginInfo> specification = this.createSpecification(loginInfo);
        PageRequest pageable = PageRequest.of(pageNo-1, pageSize);
        return  PageUtil.pageConvert(userLoginInfoRepository.findAll(specification, pageable));
    }

    
    
    
}
